{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Most Common Word"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #hash-table #string #counting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #哈希表 #字符串 #计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: mostCommonWord"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #最常见的单词"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定一个段落 (paragraph) 和一个禁用单词列表 (banned)。返回出现次数最多，同时不在禁用列表中的单词。</p>\n",
    "\n",
    "<p>题目保证至少有一个词不在禁用列表中，而且答案唯一。</p>\n",
    "\n",
    "<p>禁用列表中的单词用小写字母表示，不含标点符号。段落中的单词不区分大小写。答案都是小写字母。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例：</strong></p>\n",
    "\n",
    "<pre><strong>输入:</strong> \n",
    "paragraph = &quot;Bob hit a ball, the hit BALL flew far after it was hit.&quot;\n",
    "banned = [&quot;hit&quot;]\n",
    "<strong>输出:</strong> &quot;ball&quot;\n",
    "<strong>解释:</strong> \n",
    "&quot;hit&quot; 出现了3次，但它是一个禁用的单词。\n",
    "&quot;ball&quot; 出现了2次 (同时没有其他单词出现2次)，所以它是段落里出现次数最多的，且不在禁用列表中的单词。 \n",
    "注意，所有这些单词在段落里不区分大小写，标点符号需要忽略（即使是紧挨着单词也忽略， 比如 &quot;ball,&quot;）， \n",
    "&quot;hit&quot;不是最终的答案，虽然它出现次数更多，但它在禁用单词列表中。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= 段落长度 &lt;= 1000</code></li>\n",
    "\t<li><code>0 &lt;= 禁用单词个数 &lt;= 100</code></li>\n",
    "\t<li><code>1 &lt;= 禁用单词长度 &lt;= 10</code></li>\n",
    "\t<li>答案是唯一的, 且都是小写字母&nbsp;(即使在 <code>paragraph</code> 里是大写的，即使是一些特定的名词，答案都是小写的。)</li>\n",
    "\t<li><code>paragraph</code>&nbsp;只包含字母、空格和下列标点符号<code>!?&#39;,;.</code></li>\n",
    "\t<li>不存在没有连字符或者带有连字符的单词。</li>\n",
    "\t<li>单词里只包含字母，不会出现省略号或者其他标点符号。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [most-common-word](https://leetcode.cn/problems/most-common-word/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [most-common-word](https://leetcode.cn/problems/most-common-word/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"Bob hit a ball, the hit BALL flew far after it was hit.\"\\n[\"hit\"]', '\"a.\"\\n[]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "from collections import Counter, defaultdict\n",
    "\n",
    "class Solution(object):\n",
    "    def mostCommonWord(self, paragraph, banned):\n",
    "        \"\"\"\n",
    "        :type paragraph: str\n",
    "        :type banned: List[str]\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        # 使用正则表达式分词\n",
    "        words = re.findall(r'\\b\\w+\\b', paragraph.lower())\n",
    "        \n",
    "        # 使用Counter统计单词出现次数\n",
    "        word_counts = Counter(words)\n",
    "        \n",
    "        word_dict = {}\n",
    "        \n",
    "        max_word = \"\"\n",
    "        max_count = 0\n",
    "        \n",
    "        for word, count in word_counts.items():\n",
    "            if word not in banned:\n",
    "                word_dict[word] = count\n",
    "                # 即时更新最大出现次数的单词\n",
    "                if count > max_count:\n",
    "                    max_word = word\n",
    "                    max_count = count\n",
    "        \n",
    "        return max_word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        ban = set(banned)\n",
    "        freq = Counter()\n",
    "        word, n = \"\", len(paragraph)\n",
    "        for i in range(n + 1):\n",
    "            if i < n and paragraph[i].isalpha():\n",
    "                word += paragraph[i].lower()\n",
    "            elif word:\n",
    "                if word not in ban:\n",
    "                    freq[word] += 1\n",
    "                word = \"\"\n",
    "        maxFreq = max(freq.values())\n",
    "        return next(word for word, f in freq.items() if f == maxFreq)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        words = re.sub(\"[!?',;.]\", ' ', paragraph.lower()).split()\n",
    "        ct = Counter(word for word in words if word not in banned)\n",
    "        return max(ct, key=ct.get)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        return Counter(w for w in re.findall(r'\\w+', paragraph.lower()) if w not in set(banned)).most_common(1)[0][0]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        # 1.处理段落，全部转换为小写\n",
    "        paragraph = paragraph.lower()\n",
    "        # 2.提取单词\n",
    "        words = re.findall(r\"\\b\\w+\\b\", paragraph)\n",
    "\n",
    "        # 3.去除禁用词\n",
    "        mwords = set(words) - set(banned)\n",
    "\n",
    "        # 4.找到出现最多次数的词\n",
    "        maxt = 0\n",
    "        mword = \"\"\n",
    "        for word in mwords:\n",
    "            c = words.count(word)\n",
    "            if c > maxt:\n",
    "                maxt = c\n",
    "                mword = word\n",
    "\n",
    "        return mword"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        \n",
    "        \n",
    "        \n",
    "        # my solution ... \n",
    "        \n",
    "        s = paragraph\n",
    "        for ch in '!?\\',;.':\n",
    "            s = s.replace(ch, ' ')\n",
    "        f = {}\n",
    "        banned = set(banned)\n",
    "        for word in s.lower().split():\n",
    "            if word not in banned:\n",
    "                f[word] = f.get(word, 0) + 1\n",
    "        fmx = max(f.values())\n",
    "        return [word for word in f if f[word]==fmx][0]\n",
    "        \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        paragraph = paragraph.lower()\n",
    "        for i in string.punctuation:  # 遍历所有标点符号\n",
    "            paragraph = paragraph.replace(i, \" \")  # 标点替换成空格，防止 paragraph4 标点后无空格的情况\n",
    "        p_list = paragraph.split(\" \")\n",
    "        hash = {}\n",
    "        for p in p_list:\n",
    "            if p != \"\":\n",
    "                hash.setdefault(p, 0)\n",
    "                hash[p] += 1\n",
    "        max_times = 0\n",
    "        output = \"\"\n",
    "        for key, value in hash.items():\n",
    "            if value > max_times and key not in banned:\n",
    "                output = key\n",
    "                max_times = value\n",
    "        return output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        ban = set(banned)\n",
    "        freq = dict()\n",
    "        word, n = '', len(paragraph)\n",
    "        for i in range(n + 1):\n",
    "            if i < n and paragraph[i].isalpha():\n",
    "                word += paragraph[i].lower()\n",
    "            elif word:\n",
    "                if word not in ban:\n",
    "                    freq[word] = freq.get(word, 0) + 1\n",
    "                word = ''\n",
    "        maxFreq = max(freq.values())\n",
    "        for key in freq.keys():\n",
    "            if freq[key] == maxFreq:\n",
    "                return key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        dic = {}\n",
    "        word, n = \"\", len(paragraph)\n",
    "        for i in range(n + 1):\n",
    "            if i < n and paragraph[i].isalpha():\n",
    "                word += paragraph[i].lower()\n",
    "            elif word:\n",
    "                if word not in banned:\n",
    "                    if word not in dic:\n",
    "                        dic[word] = 1\n",
    "                    else:\n",
    "                        dic[word] += 1\n",
    "                word = \"\"\n",
    "        print(dic)\n",
    "        max_freq = max(dic.values())\n",
    "        print(max_freq)\n",
    "        for i, j in dic.items():\n",
    "            if j == max_freq:\n",
    "                return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        paragraph=paragraph.lower().replace(\",\",\" \").replace(\"!\",\" \").replace(\".\",\"\").replace(\"?\",\" \").replace(\"  \",\" \").replace(\";\",\"\").replace(\"'\",\"\").split(\" \")\n",
    "        dp=collections.Counter(paragraph)\n",
    "        for i in banned:\n",
    "            del dp[i]\n",
    "        # print(dp)\n",
    "        # print(dp(max(dp.values())))\n",
    "        for k,v in dp.items():\n",
    "            if v == max(dp.values()):\n",
    "                return k\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "      split_pattern =r'[;,\\'\\!?.\\s]\\s*' \n",
    "      s=re.split(split_pattern,paragraph)\n",
    "      d={}\n",
    "      for w in s:\n",
    "        w=w.strip(',.?!').lower()\n",
    "        if w not in banned:\n",
    "          if w not in d:\n",
    "            d[w]=1\n",
    "          else:\n",
    "            d[w]+=1\n",
    "      print(d)\n",
    "      d['']=0\n",
    "      return max(d,key=d.get)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        paragraph=paragraph.lower()\n",
    "        words=re.split(\"[!?',;.\\s]+\",paragraph)\n",
    "        words=Counter(words)\n",
    "        result=0\n",
    "        r_word=''\n",
    "        banned=set(banned)\n",
    "        for word in words.keys():\n",
    "            if word=='' or word in banned:\n",
    "                continue\n",
    "            if words[word]>result:\n",
    "                r_word=word\n",
    "                result=words[word]\n",
    "        return r_word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        paragraph = paragraph.lower()\n",
    "        paragraph = re.sub(r'[!?\\',;.]', ' ', paragraph)\n",
    "        freq = Counter(paragraph.split())\n",
    "        for ban in banned:\n",
    "            freq[ban] = 0\n",
    "        return freq.most_common(1)[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        banned = set(banned)\n",
    "        c = Counter()\n",
    "        for m in re.finditer(f'\\w+', paragraph):\n",
    "            w = m.group().lower()\n",
    "            if w not in banned:\n",
    "                c[w] += 1\n",
    "        return c.most_common(1)[0][0]\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        banned=set(banned)\n",
    "        freq=Counter()\n",
    "        word,n='',len(paragraph)\n",
    "        for i in range(n+1):\n",
    "            if i<n and paragraph[i].isalpha():\n",
    "                word+=paragraph[i].lower()\n",
    "            elif word:\n",
    "                if word not in banned:\n",
    "                    freq[word]+=1\n",
    "                word=''\n",
    "        maxF=max(freq.values())\n",
    "        return next(word for word,f in freq.items() if f==maxF)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        map1 = {}\n",
    "        paragraph1 = paragraph.lower()\n",
    "        for i in re.split(r'[^\\w]+',paragraph1):\n",
    "            if i not in map1 and i not in banned and i != '':\n",
    "                map1[i] = 1\n",
    "            elif i in map1 and i not in banned and i != '':\n",
    "                map1[i] += 1\n",
    "        \n",
    "        maxcount = max(map1.values())\n",
    "        for j in map1:\n",
    "            if map1[j] == maxcount and j not in banned:\n",
    "                return j\n",
    "                \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        p= re.findall(r'\\w+', paragraph.lower())\n",
    "        hashtable=collections.Counter(word for word in p if word not in banned)\n",
    "        \n",
    "        return max(hashtable, key=hashtable.get)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        paragraph = re.sub(r'[^\\w\\s]', ' ', paragraph)\n",
    "        words = paragraph.lower().split()\n",
    "        counter = Counter(words)\n",
    "        for word in banned:\n",
    "            counter[word.lower()] = 0\n",
    "        return counter.most_common(1)[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        signs = [\"!\",\"?\",\"'\",\",\",\";\",\".\"]\n",
    "        for i in paragraph:\n",
    "            if i in signs:\n",
    "                paragraph=paragraph.replace(i,' ')\n",
    "        paragraph_list = paragraph.lower().split()\n",
    "        p = Counter(paragraph_list)\n",
    "        for i in banned:\n",
    "            p[i] = 0\n",
    "        return p.most_common(1)[0][0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        # 1.处理段落，全部转换为小写\n",
    "        paragraph = paragraph.lower()\n",
    "        # 2.提取单词\n",
    "        words = re.findall(r\"\\b\\w+\\b\", paragraph)\n",
    "\n",
    "        # 3.去除禁用词\n",
    "        mwords = list(set(words) - set(banned))\n",
    "\n",
    "        # 4.找到出现最多次数的词\n",
    "        maxt = 0\n",
    "        mword = \"\"\n",
    "        for word in mwords:\n",
    "            c = words.count(word)\n",
    "            if c > maxt:\n",
    "                maxt = c\n",
    "                mword = word\n",
    "\n",
    "        return mword"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        all=paragraph.replace(',',' ').replace('.',' ').replace('!',' ').lower().split()\n",
    "        adict={}\n",
    "        for i in all:\n",
    "            if not i[-1].isalpha():\n",
    "                i=i[:-1]\n",
    "            if i in adict:\n",
    "                adict[i]+=1\n",
    "            else:\n",
    "                adict[i]=1\n",
    "        res=[]\n",
    "        for k,v in adict.items():\n",
    "            res.append([k,v])\n",
    "        res.sort(key=lambda x:(-x[1]))\n",
    "        for i in res:\n",
    "            if i[0] not in banned:\n",
    "                return i[0]\n",
    "            else:\n",
    "                continue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        return max(Counter(re.split(r\"[ ,.!?';]\", paragraph.lower())).items(), key=lambda x:(len(x) > 0, x[0] not in b, x[1]))[0] if (b := set(banned + [\"\"])) else \"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        words=re.findall(r'\\w+',paragraph)\n",
    "        words=[i.lower() for i in words if i.lower() not in banned]\n",
    "        c=Counter(words)\n",
    "        for k,v in c.items():\n",
    "            if v==max(c.values()):\n",
    "                return k\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "\n",
    "        res = re.findall(\"[\\w]+\", paragraph.lower())\n",
    "        # print(res)\n",
    "        d = Counter(res)\n",
    "        # print(d)\n",
    "        c = sorted(d, key = lambda x:d[x], reverse = True)\n",
    "        # print(c)\n",
    "        for i in c:\n",
    "            if i not in banned:\n",
    "                return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        paragraph = paragraph.lower()\n",
    "        string = \"\"\n",
    "        my_list = []\n",
    "        for i in range(len(paragraph)):\n",
    "            if paragraph[i].isalpha():\n",
    "                string = string+paragraph[i]\n",
    "            else:\n",
    "                if paragraph[i-1].isalpha() and (string not in banned):\n",
    "                    my_list.append(string)\n",
    "                string = \"\"\n",
    "        if paragraph[-1].isalpha() and (string not in banned):\n",
    "            my_list.append(string)\n",
    "        countdict = {}\n",
    "        for i in set(my_list):\n",
    "            count = my_list.count(i)\n",
    "            countdict[i] = count\n",
    "        for i in countdict.keys():\n",
    "            if countdict[i] == max(countdict.values()):\n",
    "                return i\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\r\n",
    "from collections import Counter\r\n",
    "class Solution:\r\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\r\n",
    "        freq = Counter()\r\n",
    "        ban = set(banned)\r\n",
    "        n = len(paragraph)\r\n",
    "        word = \"\"\r\n",
    "        res, max_freq = \"\", -1\r\n",
    "        for i in range(n + 1):\r\n",
    "            if i < n and paragraph[i].isalpha():\r\n",
    "                word += paragraph[i].lower()\r\n",
    "            elif word:\r\n",
    "                if word not in ban:\r\n",
    "                    freq[word] += 1\r\n",
    "                    if freq[word] > max_freq:\r\n",
    "                        res = word\r\n",
    "                        max_freq = freq[word]\r\n",
    "                word = \"\"\r\n",
    "        \r\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        return Counter(\n",
    "            m.group()\n",
    "            for m in re.finditer(r'\\w+',paragraph.lower()) \n",
    "            if m.group() not in banned\n",
    "        ).most_common(1)[0][0]\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        recode = Counter()\n",
    "        ban = set(banned)\n",
    "        li = []\n",
    "        word = ''\n",
    "        n = len(paragraph)\n",
    "        for i in range(n+1):\n",
    "            if i < n and paragraph[i].isalpha():\n",
    "                word += paragraph[i].lower()\n",
    "            elif word:\n",
    "                if word not in ban:\n",
    "                    recode[word] += 1\n",
    "                word = ''\n",
    "        return [word for word in recode.keys() if recode[word] == max(recode.values())][0]\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        ans = ''\n",
    "        large = 0\n",
    "        paragraph = paragraph.lower()\n",
    "        paragraph_list = re.sub('[^a-z]+',' ',paragraph).rstrip().split(' ')\n",
    "        count = Counter(paragraph_list)\n",
    "        for k,v in count.items():\n",
    "            if v > large and k not in banned:\n",
    "                large = v\n",
    "                ans = k\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        paragraph = paragraph.lower()\n",
    "        for i in string.punctuation:  # 遍历所有标点符号\n",
    "            paragraph = paragraph.replace(i, \" \")  # 标点替换成空格，防止 paragraph4 标点后无空格的情况\n",
    "        p_list = paragraph.split(\" \")\n",
    "\n",
    "        # 计数\n",
    "        # hash = {}\n",
    "        # for p in p_list:\n",
    "        #     if p != \"\":\n",
    "        #         hash.setdefault(p, 0)\n",
    "        #         hash[p] += 1\n",
    "        # 计数的另一种方法 \n",
    "        hash = collections.Counter()\n",
    "        for p in p_list:\n",
    "            if p != \"\":\n",
    "                hash[p] += 1\n",
    "\n",
    "        max_times = 0\n",
    "        output = \"\"\n",
    "        for key, value in hash.items():\n",
    "            if value > max_times and key not in banned:\n",
    "                output = key\n",
    "                max_times = value\n",
    "        return output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: list[str]) -> str:\n",
    "        ban = set(banned)\n",
    "        freq = Counter()\n",
    "        word, n = \"\", len(paragraph)\n",
    "        for i in range(n + 1):\n",
    "            if i < n and paragraph[i].isalpha():\n",
    "                word += paragraph[i].lower()\n",
    "            elif word:\n",
    "                if word not in ban:\n",
    "                    freq[word] += 1\n",
    "                word = \"\"\n",
    "        maxFreq = max(freq.values())\n",
    "        return next(word for word, f in freq.items() if f == maxFreq)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        words = []\n",
    "        word = \"\"\n",
    "        for c in paragraph:\n",
    "            if c not in \" !?',;.\":\n",
    "                word += c\n",
    "            else:\n",
    "                if word:\n",
    "                    words.append(word)\n",
    "                word = \"\"\n",
    "        words.append(word)\n",
    "        # print(words)\n",
    "        dic = {}\n",
    "        for word in words:\n",
    "            word = re.sub('[^a-zA-Z]+', '', word).lower()\n",
    "            if word not in banned:\n",
    "                if word not in dic:\n",
    "                    dic[word] = 1\n",
    "                else:\n",
    "                    dic[word] += 1\n",
    "        # print(dic)\n",
    "        return max(dic, key=lambda x: dic[x])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        paragraph = paragraph.lower()\n",
    "        paragraph_list = list()\n",
    "        ban = set(banned)\n",
    "        current_word = ''\n",
    "        for chr in paragraph:\n",
    "            if 'a' <= chr <= 'z':\n",
    "                current_word = current_word + chr\n",
    "            else:\n",
    "                if current_word != '':\n",
    "                    if current_word not in ban:\n",
    "                        paragraph_list.append(current_word)\n",
    "                    current_word = ''\n",
    "        \n",
    "        if current_word != '':\n",
    "            if current_word not in ban:\n",
    "                paragraph_list.append(current_word)\n",
    "\n",
    "        dict_count = dict()\n",
    "        for word in paragraph_list:\n",
    "            if dict_count.get(word):\n",
    "                dict_count[word] += 1\n",
    "            else:\n",
    "                dict_count[word] = 1\n",
    "        \n",
    "        max_count = max(list(dict_count.items()), key=lambda x : x[1])[1]\n",
    "        for word in dict_count:\n",
    "            if dict_count[word] == max_count:\n",
    "                return word\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        s = \"\"\"!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}\"\"\"\n",
    "        for i in s:\n",
    "            paragraph = paragraph.replace(i, \" \")\n",
    "        paragraph_l = paragraph.split()\n",
    "        dic = {}\n",
    "        for i in paragraph_l:\n",
    "            i = i.lower()\n",
    "            dic[i] = dic.get(i, 0) + 1\n",
    "        for i in banned:\n",
    "            if i in dic.keys():\n",
    "                dic.pop(i)\n",
    "        return sorted(dic.items(), key=lambda l:l[1], reverse=True)[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        a=\"',.!?;:'\"\n",
    "        for i in paragraph:\n",
    "            if i in a:\n",
    "                paragraph=paragraph.replace(i,' ')\n",
    "        b={}\n",
    "        for i in paragraph.lower().split(' '):\n",
    "            if i!='' and i not in banned:\n",
    "                if not b.get(i):\n",
    "                    b[i]=1\n",
    "                else:\n",
    "                    b[i]+=1\n",
    "        return max(b,key=b.get)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        bans = set()\n",
    "        x = \"hall\"\n",
    "        x.lower()\n",
    "        banned = list(map(lambda x: x.lower(), banned))\n",
    "        bans.update(banned)\n",
    "        print(f\"bans: {bans}\")\n",
    "        word_counts = {}\n",
    "        words = re.split(r\"\\W+\", paragraph)\n",
    "        for word in words:\n",
    "            word = word.lower()\n",
    "            if word in bans or word == \"\":\n",
    "                continue\n",
    "            if word in word_counts:\n",
    "                word_counts[word] +=  1\n",
    "            else:\n",
    "                 word_counts[word] =1\n",
    "        #print(word_counts)\n",
    "        max_key = max(word_counts, key=word_counts.get)\n",
    "        return max_key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        recode = Counter()\n",
    "        ban = set(banned)\n",
    "        li = []\n",
    "        word = ''\n",
    "        n = len(paragraph)\n",
    "        for i in range(n+1):\n",
    "            if i < n and paragraph[i].isalpha():\n",
    "                word += paragraph[i].lower()\n",
    "            elif word:\n",
    "                if word not in ban:\n",
    "                    recode[word] += 1\n",
    "                word = ''\n",
    "        return [word for word in recode.keys() if recode[word] == max(recode.values())][0]\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "\n",
    "        ds = {}\n",
    "        w = ''\n",
    "        ss = {' ', \"'\", '!', '?', ',',';','.'}\n",
    "\n",
    "        def put(w):\n",
    "            if w in banned: return\n",
    "            if w not in ds:\n",
    "                ds[w] = 1\n",
    "            else:\n",
    "                ds[w] += 1\n",
    "\n",
    "        for c in paragraph:\n",
    "            if c in ss:\n",
    "                if w != '':\n",
    "                    put(w)\n",
    "                    w = ''\n",
    "            else:\n",
    "                w += c.lower()\n",
    "        if w != '': put(w)\n",
    "        mx = 0\n",
    "        res = None\n",
    "        for k,v in ds.items():\n",
    "            if v > mx:\n",
    "                res = k\n",
    "                mx = v\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        p=re.findall(r'\\w+', paragraph.lower())\n",
    "        hashtable=collections.Counter(word for word in p if word not in banned)\n",
    "        maxvalue=max(hashtable.values())\n",
    "        st=[key for key,value in hashtable.items() if value==maxvalue]\n",
    "        return st[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        words=[word.lower() for word in re.findall(r'[a-zA-Z]+',paragraph)]\n",
    "\n",
    "        s=Counter(words).most_common(len(banned)+1)\n",
    "        for i in range(len(banned)+1):\n",
    "            if s[i][0] not in banned:\n",
    "                return s[i][0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        # 首先获取单词列表\n",
    "        for i in paragraph:\n",
    "            if not i.isalnum() and not i.isspace():\n",
    "                paragraph = paragraph.replace(i, \" \")\n",
    "        paragraph = paragraph.lower().split()\n",
    "        # 遍历单词列表中的字符串, 统计次数, 根据次数进行排序, 返回最多的单词\n",
    "        dct = {}\n",
    "        for i in paragraph:\n",
    "            dct[i] = dct.get(i, 0) + 1\n",
    "        dct = sorted(dct.items(), key=lambda x:x[1], reverse=True)\n",
    "        for i,_ in dct:\n",
    "            if i not in banned:\n",
    "                return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        ban = set(banned)\n",
    "        freq = Counter()\n",
    "        word , n = \"\" , len(paragraph) \n",
    "        for i in range(n+1) :\n",
    "            if i < n and paragraph[i].isalpha() :\n",
    "                word += paragraph[i].lower()  # 拼接单词\n",
    "            elif word : # 检查拼成的单词\n",
    "                if word not in ban:\n",
    "                    freq[word] += 1\n",
    "                word = \"\" # 清空\n",
    "        maxFreq = max(freq.values())\n",
    "        return next(word for word,f in freq.items() if f == maxFreq)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        signal = \"!?',;.\"\n",
    "        new_paragraph = [s if s not in signal else ' ' for s in paragraph]\n",
    "        list1 = ''.join(new_paragraph).lower().split(' ')\n",
    "        print(list1)\n",
    "        count = Counter(list1).most_common()\n",
    "        print(count)\n",
    "        for word, value in count:\n",
    "            print(word, value)\n",
    "            if word in banned or word == '':\n",
    "                continue\n",
    "            return word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        for c in \"!?',;.\":\n",
    "            paragraph=paragraph.replace(c,' ')\n",
    "        word_list=paragraph.split()\n",
    "        hash_table={}\n",
    "        for w in word_list:\n",
    "            if w.lower() not in banned:\n",
    "                if w.lower() not in hash_table:\n",
    "                    hash_table[w.lower()]=1\n",
    "                else:\n",
    "                    hash_table[w.lower()]+=1\n",
    "        result=sorted(hash_table.items(),key=lambda x:-x[1])\n",
    "        return result[0][0]\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        return max(Counter(re.split(r\"[ ,.!?';]\", paragraph.lower())).items(), key=lambda x:(len(x) > 0, x[0] not in b, x[1]))[0] if (b := set(banned + [\"\"])) else \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        ban = set(banned)\n",
    "        freq = Counter()\n",
    "        word, n = \"\", len(paragraph)\n",
    "        for i in range(n + 1):\n",
    "            if i < n and paragraph[i].isalpha():\n",
    "                word += paragraph[i].lower()\n",
    "            elif word:\n",
    "                if word not in ban:\n",
    "                    freq[word] += 1\n",
    "                word = \"\"\n",
    "        maxFreq = max(freq.values())\n",
    "        return next(word for word, f in freq.items() if f == maxFreq)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        words = Counter()\n",
    "        word = ''\n",
    "        for s in paragraph:\n",
    "            if s.isalpha():\n",
    "                word+=s.lower()\n",
    "            elif word:\n",
    "                if word not in banned:\n",
    "                    words[word]+=1\n",
    "                word=''\n",
    "        if word:\n",
    "            if word not in banned:\n",
    "                words[word]+=1\n",
    "        maxnum = max(words.values())\n",
    "        return [word for word,v in words.items() if v==maxnum][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        a,b,d=\"\",[],{}\n",
    "        # if not \" \" in paragraph:\n",
    "        #     return paragraph\n",
    "        # c=paragraph.split(\" \")\n",
    "        for i in range(len(paragraph)):\n",
    "            if \"a\" <= paragraph[i] <= \"z\" or \"A\" <= paragraph[i] <= \"Z\":\n",
    "\n",
    "                a+=paragraph[i]\n",
    "                if i == len(paragraph)-1:\n",
    "                    b.append(a.lower()) if a else b\n",
    "\n",
    "            else:\n",
    "                b.append(a.lower()) if a else b\n",
    "                a=\"\"\n",
    "\n",
    "            # # print(i[-1])\n",
    "\n",
    "            # if \",\" not in i and \".\" not in i and \"!\" not in i and \"?\" not in i: \n",
    "            #     b.append(i.lower())\n",
    "            # elif len(i.split(\",\")) >2:\n",
    "            #     for k in i:\n",
    "            #         if \"a\" <= k <= \"z\":\n",
    "            #             b.append(k)\n",
    "            #     print(b)\n",
    "            # else:\n",
    "            #     b.append(i[:-1].lower()) #if \",\" not in i #else i[:-1].lower()\n",
    "        print(b)\n",
    "        for j in b:\n",
    "            if j not in banned and j not in d:\n",
    "                d[b.count(j)]=j\n",
    "        return d[max(d)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        paragraph=paragraph.lower()\n",
    "        paragraph=list(paragraph)\n",
    "        for i in range(len(paragraph)):\n",
    "            if paragraph[i]==',':\n",
    "                paragraph[i]=' '\n",
    "            if paragraph[i].isalpha()==False and paragraph[i]!=' ':\n",
    "                paragraph[i]=''\n",
    "        paragraph=''.join(paragraph)\n",
    "        paragraph=paragraph.split()\n",
    "        print(paragraph)\n",
    "        for i in range(len(banned)):\n",
    "            while banned[i] in paragraph:\n",
    "                paragraph.remove(banned[i])\n",
    "        temp1=collections.Counter(paragraph)\n",
    "        max_key = max(temp1, key=temp1.get)\n",
    "        answer=max_key\n",
    "        return answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        list_paragraph = paragraph.lower().replace(',', ' ').replace('.', '').replace('!', '').replace('?', '').replace(\n",
    "            ';', '').replace(\"'\", '').lower().split()\n",
    "        count_paragraph = Counter(list_paragraph).most_common()\n",
    "        banned = set(banned)\n",
    "        for ele, i in count_paragraph:\n",
    "            if ele not in banned:\n",
    "                return ele\n",
    "                break\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        freq = {}\n",
    "        word = \"\"\n",
    "        for i in range(len(paragraph)+1):\n",
    "            if i<len(paragraph) and paragraph[i].isalpha():\n",
    "                word += paragraph[i].lower()\n",
    "            elif word:\n",
    "                if word not in banned :\n",
    "                    if word in freq:\n",
    "                        freq[word] += 1\n",
    "                    else:\n",
    "                        freq[word] = 1\n",
    "                word = \"\"\n",
    "        maxfreq = max(freq.values())\n",
    "        for key,val in freq.items():\n",
    "            if val == maxfreq:\n",
    "                return key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        para_dict = collections.defaultdict(int)\n",
    "\n",
    "        word = ''\n",
    "        for char in paragraph:\n",
    "            if char.isalpha():\n",
    "                word += char.lower()\n",
    "            else:\n",
    "                if word and word not in banned:\n",
    "                    para_dict[word] += 1\n",
    "                word = ''\n",
    "\n",
    "        if word:\n",
    "            para_dict[word] += 1\n",
    "\n",
    "        para_dict = dict(sorted(para_dict.items(), key=lambda x: x[1], reverse=True))\n",
    "\n",
    "        for word in para_dict.keys():\n",
    "            if word not in banned:\n",
    "                return word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        num=\"\"\n",
    "        d={}\n",
    "        for i in paragraph:\n",
    "            if i not in \",.?%!;'\":\n",
    "                num=num+i.lower()\n",
    "            else:\n",
    "                i=\" \"\n",
    "                num=num+i\n",
    "        num_list=num.split(\" \")\n",
    "        for i in num_list:\n",
    "            if i not in \" \":\n",
    "                if i not in banned:\n",
    "                    if i not in d:\n",
    "                        d[i]=1\n",
    "                    else:\n",
    "                        d[i]=d[i]+1\n",
    "        list_1 = list(d.items())\n",
    "       \n",
    "        list_1.sort(key=lambda x: x[1], reverse=True)\n",
    "        \n",
    "        return list_1[0][0]\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        paragraph=paragraph.lower()\n",
    "        paragraph=list(paragraph)\n",
    "        for i in range(len(paragraph)):\n",
    "            if paragraph[i]==',':\n",
    "                paragraph[i]=' '\n",
    "            if paragraph[i].isalpha()==False and paragraph[i]!=' ':\n",
    "                paragraph[i]=''\n",
    "        paragraph=''.join(paragraph)\n",
    "        paragraph=paragraph.split()\n",
    "        print(paragraph)\n",
    "        for i in range(len(banned)):\n",
    "            while banned[i] in paragraph:\n",
    "                paragraph.remove(banned[i])\n",
    "        temp1=collections.Counter(paragraph)\n",
    "        max_key = max(temp1, key=temp1.get)\n",
    "        answer=max_key\n",
    "        return answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        s=paragraph.lower()\n",
    "        lst=re.findall(r\"\\w+\",s)\n",
    "        dic={}\n",
    "        for x in lst:\n",
    "            if x not in banned:\n",
    "                if x not in dic:\n",
    "                    dic[x]=1\n",
    "                else:\n",
    "                    dic[x]+=1\n",
    "        ans=sorted(dic.items(),key=lambda x:x[1],reverse=True)\n",
    "        return ans[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, s: str, strs: List[str]) -> str:\n",
    "        ban = set(strs)\n",
    "        freq = Counter()\n",
    "        word, n = \"\", len(s)\n",
    "        for i in range(n + 1):\n",
    "            if i < n and s[i].isalpha():\n",
    "                word += s[i].lower()\n",
    "            elif word:\n",
    "                if word not in strs:\n",
    "                    freq[word] += 1\n",
    "                word = \"\"\n",
    "        maxFreq = max(freq.values())\n",
    "        return next(word for word, f in freq.items() if f == maxFreq)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "            templist = paragraph.replace(\",\",\" \").replace(\";\",\" \").replace(\"'\",\" \").replace(\"!\",\" \").replace(\".\",\" \").replace(\"?\",\" \").lower().split()\n",
    "            tempdict = {}\n",
    "            for i in templist:\n",
    "                if i in banned:\n",
    "                    continue\n",
    "                if i in tempdict.keys():\n",
    "                    tempdict[i] +=1\n",
    "                else:\n",
    "                    tempdict[i] = 1\n",
    "            tempmaxvalue = max(tempdict.values())\n",
    "            tempmaxres = []\n",
    "            for i in list(tempdict.items()):\n",
    "                if i[1] == tempmaxvalue:\n",
    "                    tempmaxres.append(i[0])\n",
    "            return \"\".join(tempmaxres)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        words = [word for word in re.findall(r'\\w+',paragraph.lower())]\n",
    "        word_map = sorted(Counter(words).items(), key=lambda x: x[1], reverse = True)\n",
    "        for word, _ in word_map:\n",
    "            if word not in set(banned):\n",
    "                return word\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "from collections import  Counter\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        return Counter([i for i in re.sub(\"[!?',;.]\",' ',paragraph.lower()).split() if i not in [j.lower() for j in banned]]).most_common()[0][0]\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\r\n",
    "        import re\r\n",
    "        remove_char = \"[!?',;.]\"\r\n",
    "        ans = str()\r\n",
    "        frequency = 0\r\n",
    "        paragraph = paragraph.lower()\r\n",
    "        a = re.sub(remove_char,' ',paragraph)\r\n",
    "        condition = a.split(' ')\r\n",
    "        for i in condition:\r\n",
    "            if i not in banned and i != '':\r\n",
    "                if condition.count(i) > frequency:\r\n",
    "                    frequency = condition.count(i)\r\n",
    "                    ans = i\r\n",
    "        return ans\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        words = paragraph.replace('!',' ').replace('?',' ').replace(\"'\",' ').replace(',',' ').replace(';',' ').replace('.',' ').replace('  ',' ').strip().lower().split(' ')\n",
    "        dic = {}\n",
    "        for word in words:\n",
    "            if word not in banned:\n",
    "                if word in dic:\n",
    "                    dic[word] += 1\n",
    "                else:\n",
    "                    dic[word] = 1\n",
    "        maxNum = 0\n",
    "        mostCommon = ''\n",
    "        print(dic)\n",
    "        for word in dic:\n",
    "            if dic[word] > maxNum:\n",
    "                mostCommon = word\n",
    "                maxNum = dic[word]\n",
    "        return mostCommon\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        replace_char = [\"!\",\"?\",\"'\",\",\",\".\",\";\"]\n",
    "        for x in replace_char:\n",
    "            paragraph = paragraph.replace(x,' ')\n",
    "        paragraph =paragraph.lower()\n",
    "        paragraph_list = paragraph.split() \n",
    "        words = []\n",
    "        for x in paragraph_list:\n",
    "            if x not in words:\n",
    "                words.append(x)\n",
    "        print(paragraph_list)\n",
    "        print(words)\n",
    "\n",
    "        max_num = 0\n",
    "        flag = ''\n",
    "        for word in words:\n",
    "            if word in banned:\n",
    "                continue\n",
    "            num = paragraph_list.count(word)\n",
    "            if num > max_num :\n",
    "                max_num = num \n",
    "                flag = word\n",
    "        return flag\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "class Solution:\n",
    "    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:\n",
    "        def testzimu(s1):\n",
    "            s1 = s1.group(0)\n",
    "            print(s1)\n",
    "            if s1.isalpha() or s1==\" \":\n",
    "                return s1\n",
    "            else:\n",
    "                return \" \"    \n",
    "        paragraph=re.sub('(.+?)',testzimu,paragraph).replace(\"  \",\" \")\n",
    "        list1 =[x.lower() for x in paragraph.split(\" \")]\n",
    "        dict1 = {}\n",
    "        for i in list1:\n",
    "            if dict1.get(i)==None:\n",
    "                dict1[i] = 1\n",
    "            else:\n",
    "                dict1[i]+=1\n",
    "        for i in banned:\n",
    "            if i in dict1.keys():\n",
    "                 dict1.pop(i)\n",
    "        if dict1.get(\"\")!=None:\n",
    "            dict1.pop(\"\")\n",
    "        dict1 = sorted(dict1.items(),key=lambda x:x[1],reverse=True)\n",
    "        return dict1[0][0]"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
